package com.cjm.algorithm.quick;

import com.cjm.algorithm.HelpUtils;

public class QuickSortCopy1 {
    public static void sort(int[] nums) {
        sort(nums, 0, nums.length - 1);
    }

    public static void sort(int[] nums, int left, int right) {
        if (left > right) return;
        int p = partition(nums, left, right);
        sort(nums, left, p - 1);
        sort(nums, p + 1, right);
    }

    private static int partition(int[] nums, int left, int right) {
        int flag = nums[left];
        int start = left;
        for (int i = left + 1; i <= right; i++) {
            if (nums[i] < flag) {
                start++;
                HelpUtils.swap(nums, start, i);
            }
        }
        HelpUtils.swap(nums, start, left);
        return start;
    }

    public static void main(String[] args) {
        int[] arr = new int[] {4, 6, 2, 8, 4, 1, 5, 7, 96};
        sort(arr);
        for (int i : arr) {
            System.err.println(i);
        }
    }

}
